package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping
    @ApiOperation("新增菜品")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品{}", dishDTO);
        dishService.addDish(dishDTO);
        //缓存优化----清理缓存
        redisTemplate.delete("dish_"+dishDTO.getCategoryId());
        return Result.success();
    }
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result pageList(DishPageQueryDTO dto){
        log.info("菜品分页查询：{}",dto);
        PageResult result= dishService.page(dto);
        return Result.success(result);
    }
    @ApiOperation("删除菜品")
    @DeleteMapping
    public Result deleteDish( @RequestParam List<Long> ids){
        log.info("删除菜品{}",ids);
        dishService.deleteDish(ids);
        //缓存优化----清理缓存--将菜品缓存全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    @ApiOperation("菜品回显")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        log.info("回显菜品{}",id);
        DishVO dishVO= dishService.getById(id);
        return Result.success(dishVO);
    }
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dto){
        log.info("修改菜品{}", dto);
        dishService.update(dto);
        //缓存优化----清理缓存--将菜品缓存全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result getByCategoryId(Long categoryId){
       List<DishVO> dish= dishService.getByCategoryId(categoryId);
        return Result.success(dish);
    }
    @ApiOperation("菜品起售停售")
    @PostMapping("/status/{status}")
    public Result updateDishStatus(@PathVariable Long status,Long id){
        dishService.updateDihsStauts(status,id);
        //缓存优化----清理缓存--将菜品缓存全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);
        return Result.success();
    }
}
